$( document ).ready( function(){

    users();

    patients();

    doctors();

    settings();

    reminders();

    clinics();

    loginout();

    changepwd();
    
} );

var GENPATH = "api/index.php/";

/**USERS***********************************************************************/
function users(){
  var oUrl = GENPATH + "users_drv/user/";

  $('#btn_users').on('click', function(){
    var oData = {};
    oData.id = $('#user_id').val();
    doSCPAjax(oUrl, oData, "GET");
  });

  $('#btn_users_all').on('click', function(){
    var oData = {};
    if($('#u_profile').val() != ""){
      oData.profile = $('#u_profile').val();
    }

    oData.name = $('#u_name').val();
    oData.email = $('#u_email').val();
    oData.phone_number = $('#u_phone_number').val();
    
    if( $('#u_bottom_row').val() != "" && $('#u_top_row').val() != "" ) {
      oData.bottom_row = $('#u_bottom_row').val();
      oData.top_row = $('#u_top_row').val();
    }
    doSCPAjax(GENPATH + "users_drv/users", oData, "GET");
  });

  $('#btn_users_post').on('click', function(){
    var oData = {};
    oData.profile = $('#u_profile').val();
    oData.name = $('#u_name').val();
    oData.email = $('#u_email').val();
    oData.password = $('#u_password').val();
    oData.phone_number = $('#u_phone_number').val();
    oData.last_login = $('#u_last_login').val();
    oData.last_change_pwd = $('#u_last_change_pwd').val();
    oData.patient_id = $('#u_patient_id').val();
    oData.doctor_id = $('#u_doctor_id').val();
    doSCPAjax(oUrl, oData, "POST");
  });

  $('#btn_users_put').on('click', function(){
    var oData = {};
    oData.id = $('#user_id').val();
    oData.profile = $('#u_profile').val();
    oData.name = $('#u_name').val();
    oData.email = $('#u_email').val();
    oData.password = $('#u_password').val();
    oData.phone_number = $('#u_phone_number').val();
    oData.last_login = $('#u_last_login').val();
    oData.last_change_pwd = $('#u_last_change_pwd').val();
    oData.patient_id = $('#u_patient_id').val();
    oData.doctor_id = $('#u_doctor_id').val();
    doSCPAjax(oUrl, oData, "PUT");
  });

  $('#btn_users_del').on('click', function(){
    var oData = {};
    oData.id = $('#user_id').val();
    doSCPAjax(oUrl, oData, "DELETE");
  });
}
/**USERS***********************************************************************/

/**PATIENTS********************************************************************/
function patients(){
  var oUrl = GENPATH + "patients_drv/patient/";

  $('#btn_patients').on('click', function(){
    var oData = {};
    oData.id = $("#patient_id").val();

    doSCPAjax( oUrl, oData, "GET");
  });

  $('#btn_patients_all').on('click', function(){
    
    var oData = {};

    
    oData.name = $('#p_name').val();
    
    if( $('#p_state').val() != ""){
      oData.state = $('#p_state').val();
    }
    if( $('#p_phone_number_1').val() != ""){
      oData.phone_number = $('#p_phone_number_1').val();
    }
    
    oData.email = $('#p_email').val();
    
    if($('#p_auto').is(':checked')){
      oData.autocomplete = true;
    }
    if( $('#p_bottom_row').val() != "" && $('#p_top_row').val() != "" ) {
      oData.bottom_row = $('#p_bottom_row').val();
      oData.top_row = $('#p_top_row').val();
    }
    doSCPAjax( GENPATH + "patients_drv/patients/", oData, "GET");
  });

  $('#btn_patients_post').on('click', function(){
    var oData = {};

    oData.name = $('#p_name').val();
    oData.birth_date = $('#p_birthdate').val();
    oData.address = $('#p_address').val();
    oData.city = $('#p_city').val();
    oData.state = $('#p_state').val();
    oData.phone_number_1 = $('#p_phone_number_1').val();
    oData.phone_number_2 = $('#p_phone_number_2').val();
    oData.phone_number_3 = $('#p_phone_number_3').val();
    oData.email = $('#p_email').val();

    doSCPAjax( oUrl, oData, "POST");
  });

  $('#btn_patients_put').on('click', function(){
    var oData = {};

    oData.id = $('#patient_id').val();
    oData.name = $('#p_name').val();
    oData.birth_date = $('#p_birthdate').val();
    oData.address = $('#p_address').val();
    oData.city = $('#p_city').val();
    oData.state = $('#p_state').val();
    oData.phone_number_1 = $('#p_phone_number_1').val();
    oData.phone_number_2 = $('#p_phone_number_2').val();
    oData.phone_number_3 = $('#p_phone_number_3').val();
    oData.email = $('#p_email').val();

    doSCPAjax( oUrl, oData, "PUT");
  });

  $('#btn_patients_del').on('click', function(){
    var oData = {};

    oData.id = $('#patient_id').val();

    doSCPAjax( oUrl, oData, "DELETE");
  });
}
/**PATIENTS********************************************************************/

/**DOCTORS*********************************************************************/
function doctors(){
  var oUrl = GENPATH + "doctors_drv/doctor/";

  $('#btn_doctors').on('click', function(){
    var oData = {};
    oData.id = $('#doctor_id').val();
    doSCPAjax(oUrl, oData, "GET");

  });

  $('#btn_doctors_all').on('click', function(){
    var oData = {};
    
    oData.name = $('#d_name').val();
    
    if($('#d_state').val() != ""){
      oData.state = $('#d_state').val();
    }
    
    oData.phone_number = $('#d_phone_number_1').val();
    
    
    oData.email = $('#d_email').val();
    
    
    oData.specialty = $('#d_specialty').val();
    
    if($('#d_auto').is(':checked')){
      oData.autocomplete = true;
    }
    if( $('#d_bottom_row').val() != "" && $('#d_top_row').val() != "" ) {
      oData.bottom_row = $('#d_bottom_row').val();
      oData.top_row = $('#d_top_row').val();
    }
    doSCPAjax(GENPATH + "doctors_drv/doctors", oData ,"GET");
  });

  $('#btn_doctors_post').on('click', function(){
    var oData = {};
    oData.name = $('#d_name').val();
    oData.birth_date = $('#d_birth_date').val();
    oData.address = $('#d_address').val();
    oData.city = $('#d_city').val();
    oData.state = $('#d_state').val();
    oData.phone_number_1 = $('#d_phone_number_1').val();
    oData.phone_number_2 = $('#d_phone_number_2').val();
    oData.phone_number_3 = $('#d_phone_number_3').val();
    oData.fax = $('#d_fax').val();
    oData.email = $('#d_email').val();
    oData.specialty = $('#d_specialty').val();
    doSCPAjax(oUrl, oData, "POST");
  });

  $('#btn_doctors_put').on('click', function(){
    var oData = {};
    oData.id = $('#doctor_id').val();
    oData.name = $('#d_name').val();
    oData.birth_date = $('#d_birth_date').val();
    oData.address = $('#d_address').val();
    oData.city = $('#d_city').val();
    oData.state = $('#d_state').val();
    oData.phone_number_1 = $('#d_phone_number_1').val();
    oData.phone_number_2 = $('#d_phone_number_2').val();
    oData.phone_number_3 = $('#d_phone_number_3').val();
    oData.fax = $('#d_email').val();
    oData.email = $('#d_email').val();
    oData.specialty = $('#d_specialty').val();
    doSCPAjax(oUrl, oData, "PUT");
  });

  $('#btn_doctors_del').on('click', function(){
    var oData = {};
    oData.id = $('#doctor_id').val();
    doSCPAjax(oUrl, oData, "DELETE");
  });
}
/**DOCTORS*********************************************************************/

/**SETTINGS********************************************************************/
function settings(){
  var oUrl = GENPATH + "settings_drv/settings/";
  
  $("#btn_settings").on('click',function(){
    var oData = {};
    
    doSCPAjax( oUrl, oData, "GET" );
    
  });
  $("#btn_settings_upd").on('click',function(){

    var oData = {};

    if($('#smtp_server').val() != ''){
      oData.smtp_server = $('#smtp_server').val();
    }
    if($('#port').val() != ''){
      oData.port = $('#port').val();
    }
    if($('#use_ssl').val() != ''){
      oData.use_ssl = $('#use_ssl').val();
    }
    if($('#user').val() != ''){
      oData.user = $('#user').val();
    }
    if($('#password').val() != ''){
      oData.password = $('#password').val();
    }
    doSCPAjax( oUrl, oData, "PUT" );
    
  });
}
/**SETTINGS********************************************************************/

/**REMINDERS********************************************************************/
function reminders(){
  var oUrl = GENPATH + "reminders_drv/reminder/";

  $("#btn_reminders").on('click',function(){
    var oData = {};
    oData.id = $("#reminder_id").val();

    doSCPAjax( oUrl, oData, "GET" );

  });
  $("#btn_reminders_post").on('click',function(){
    var oData = {};
    oData.id = $("#reminder_id").val();

    if( $('#reminder_date').val() != ""){
      oData.reminder_date = $('#reminder_date').val();
    }
    if( $('#r_notes').val() != ""){
      oData.notes = $('#r_notes').val();
    }
    doSCPAjax( oUrl, oData, "POST" );
    
  });
  $("#btn_reminders_all").on('click',function(){
    var oData = {};
    
   
    oData.register_date_start = $('#register_date_start').val();
   
    oData.register_date_end = $('#register_date_end').val();
    
    oData.reminder_date_start = $('#reminder_date_start').val();
    
    oData.reminder_date_end = $('#reminder_date_end').val();
    
    
    oData.notes = $('#ra_notes').val();
    

    doSCPAjax( GENPATH + "reminders_drv/reminders/", oData, "GET" );
    
  });
  $("#btn_reminders_del").on('click',function(){
    var oData = {};
    oData.id = $("#reminder_id").val();
    doSCPAjax( oUrl, oData, "DELETE" );
  });
}

/**REMINDERS********************************************************************/

/**CLINICS********************************************************************/
function clinics(){
  var oUrl = GENPATH + "clinics_drv/clinic/";

  $('#btn_clinics').on('click', function(){
    var oData = {};
    oData.id = $("#clinic_id").val();

    doSCPAjax( oUrl, oData, "GET" );
  });
  $('#btn_clinics_all').on('click', function(){
    var oData = {};
    
    oData.name = $('#name').val();
    oData.address = $('#address').val();
    oData.city = $('#city').val();
    oData.state = $('#state').val();
    oData.phone_number = $('#phone_number').val();
    oData.email = $('#email').val();
    oData.fax = $('#fax').val();
    oData.notes = $('#notes').val();
    
    if( $('#clinic_bottom_row').val() != "" && $('#clinic_top_row').val() != "" ) {
      oData.bottom_row = $('#clinic_bottom_row').val();
      oData.top_row = $('#clinic_top_row').val();
    }
    console.log('Filtros ');
    console.log(oData);
    doSCPAjax( GENPATH + "clinics_drv/clinics/", oData, 'GET' );
    
  });
  $('#btn_clinics_post').on('click', function(){
    var oData = {};

    if( $('#name').val() != "" ){
      oData.name = $('#name').val();
    }
    if( $('#address').val() != "" ){
      oData.address = $('#address').val();
    }
    if( $('#city').val() != "" ){
      oData.city = $('#city').val();
    }
    if( $('#state').val() != "" ){
      oData.state = $('#state').val();
    }
    if( $('#phone_number').val() != "" ){
      oData.phone_number = $('#phone_number').val();
    }
    if( $('#email').val() != "" ){
      oData.email = $('#email').val();
    }
    if( $('#fax').val() != "" ){
      oData.fax = $('#fax').val();
    }
    if( $('#notes').val() != "" ){
      oData.notes = $('#notes').val();
    }

    doSCPAjax( oUrl, oData, 'POST' );
    
  });
  $('#btn_clinics_put').on('click', function(){
    var oData = {};

    oData.id = $("#clinic_id").val();

    if( $('#name').val() != "" ){
      oData.name = $('#name').val();
    }
    if( $('#address').val() != "" ){
      oData.address = $('#address').val();
    }
    if( $('#city').val() != "" ){
      oData.city = $('#city').val();
    }
    if( $('#state').val() != "" ){
      oData.state = $('#state').val();
    }
    if( $('#phone_number').val() != "" ){
      oData.phone_number = $('#phone_number').val();
    }
    if( $('#email').val() != "" ){
      oData.email = $('#email').val();
    }
    if( $('#fax').val() != "" ){
      oData.fax = $('#fax').val();
    }
    if( $('#notes').val() != "" ){
      oData.notes = $('#notes').val();
    }

    doSCPAjax( oUrl, oData, 'PUT' );
  });
  $('#btn_clinics_del').on('click', function(){
    var oData = {};
    oData.id = $("#clinic_id").val();

    doSCPAjax( oUrl, oData, "DELETE" );
  });
}

/**CLINICS********************************************************************/

/**LOG IN OUT********************************************************************/

function loginout(){
  
  $('#btn_login').on( 'click', function(){
    var oData = {};
    oData.email = $("#l_email").val();
    oData.password = $("#l_password").val();
    doSCPAjax( GENPATH + "login_drv/log_in/", oData, "POST" );
  });
  $('#btn_logout').on( 'click', function(){
    var oData = {};
    doSCPAjax( GENPATH + "login_drv/log_out/", oData, "POST" );
  });

}

/**LOG IN OUT********************************************************************/

/**Change password**************************************************************/

  function changepwd(){
    $('#btn_chpwd').on('click', function(){
      var oData = {};
      oData.email = $("#chemail").val();
      oData.currentpwd = $("#cpwd").val();
      oData.newpwd = $("#npwd").val();
      doSCPAjax(GENPATH + "login_drv/change/", oData, "POST");
    });
  }

/*******************************************************************************/

function doSCPAjax( oUrl, oData, METHOD ){
  $("#result .url span").html(oUrl);
  $.ajax( {
    url: oUrl,
    data: oData,
    dataType: "json",
    type: METHOD
  }).done(function(data){
      var jsonstr = printJson(data);
      $("#result .data").html(jsonstr);
  }).fail(function( jqXHR, textStatus, errorThrown){
      console.log(jqXHR);
      console.log(textStatus);      
      console.log(errorThrown);
      $("#result .data").html(jqXHR + "<br>" +textStatus + "<br/>" + errorThrown );
  });
}

function printJson( obj ){
    var data = "";
    if( obj )
        for (var key in obj) {
            if (typeof obj[key] == "object")
                data += printJson(obj[key]);
            else if (typeof obj[key] != "function")
            {
                console.log( key + ": " + obj [key] );
                data += key + ": " + obj[key] + " <br>";
            }
        }

    return data;

}

function isInt(n){
        return Number(n)===n && n%1===0;
}